import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
    state: {
        id: '',
        userName: '',
        realName: '',
        icon: '',
        token: getToken()
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_userName: (state, userName) => {
            state.userName = userName
        },
        SET_realName: (state, realName) => {
            state.realName = realName
        },
        SET_icon: (state, icon) => {
            state.icon = icon
        },
        SET_id: (state, id) => {
            state.id = id
        }

    },
    actions: {
        changeToken: ({ commit }, param) => {
            if (param) {
                commit("SET_TOKEN", param);
                setToken(param)
            }
        },
        changeUserInfo: ({ commit }, param) => {
            if ('undefined' !== typeof param['userName']) {
                commit("SET_userName", param.userName);
            }
            if ('undefined' !== typeof param['realName']) {
                commit("SET_realName", param.realName);
            }
        },
        // 退出系统
        LogOut({ commit }) {
            removeToken()
            commit('SET_TOKEN', null)
            commit("SET_userName", '');
            commit("SET_realName", '');

        },
    }
}

export default user